package com.eascs.web.api.util;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.eascs.common.assertion.util.AssertErrorUtils;
import com.eascs.common.util.ApplicationContextUtil;
import com.eascs.web.api.ApiMethod;
import com.eascs.web.api.ApiMethodInvokeObject;

public class ApiMethodInvokeObjectContext {

	private static Map<String, ApiMethodInvokeObject> methodInvokeObjectMap = null;

	public static Map<String, ApiMethodInvokeObject> getMethodInvokeObjectMap() {
		if (null == methodInvokeObjectMap) {
			Map<String, ApiMethodInvokeObject> maps = new HashMap<>();
			List<Method> methods = ApplicationContextUtil.getAnnotationMethods(ApiMethod.class);
			for (Method method : methods) {
				ApiMethod apiMethod = method.getAnnotation(ApiMethod.class);
				if (null == apiMethod)
					continue;
				String name = apiMethod.name();
				if (ApiMethod.DEFAULT.equals(name)) {
					name = method.getName();
				}
				AssertErrorUtils.assertNull(maps.put(name, new ApiMethodInvokeObject(method, ApplicationContextUtil.getBean(method.getDeclaringClass()), apiMethod)), "repetitive method [{}]", name);
			}
			methodInvokeObjectMap = maps;
		}
		return methodInvokeObjectMap;
	}
}
